requireNonNull
Checks that the specified object reference is not null
. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
public Foo(Bar bar) { this.bar = Objects.requireNonNull(bar); }
Content copied to clipboard
Return
obj
if not null
Parameters
obj
the object reference to check for nullity
<T>
the type of the reference
Throws
if obj
is null
Checks that the specified object reference is not null
and throws a customized NullPointerException if it is. This method is designed primarily for doing parameter validation in methods and constructors with multiple parameters, as demonstrated below:
public Foo(Bar bar, Baz baz) { this.bar = Objects.requireNonNull(bar, "bar must not be null"); this.baz = Objects.requireNonNull(baz, "baz must not be null"); }
Content copied to clipboard
Return
obj
if not null
Parameters
obj
the object reference to check for nullity
message
detail message to be used in the event that a
NullPointerException
is thrown
<T>
the type of the reference
Throws
if obj
is null